/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.enforcement.integral.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.enforcement.integral.dto.RiskIntegralRecordDTO;
import com.hopes.bizz.enforcement.integral.entity.RiskIntegralRecordEntity;
import com.hopes.bizz.enforcement.integral.service.RiskIntegralRecordService;
import com.hopes.bizz.enforcement.integral.vo.RiskIntegralRecordVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.boss.common.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 个人积分记录
 *
 * @author daijie
 * @date 2023-07-24 09:30:02
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskIntegralRecord")
@Tag(description = "riskIntegralRecord", name = "个人积分记录管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskIntegralRecordController {

    private final RiskIntegralRecordService riskIntegralRecordService;

    /**
     * 分页查询
     *
     * @param page               分页对象
     * @param riskIntegralRecord 个人积分记录
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('basic_integral_record_view')")
    public R getriskIntegralRecordPage(Page page, RiskIntegralRecordDTO riskIntegralRecord) {
        LambdaQueryWrapper<RiskIntegralRecordEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(RiskIntegralRecordEntity::getStatisticalDate);
        wrapper.eq(ObjectUtil.isNotNull(riskIntegralRecord.getEventName()), RiskIntegralRecordEntity::getEventName, riskIntegralRecord.getEventName());
        wrapper.eq(ObjectUtil.isNotNull(riskIntegralRecord.getAccountId()), RiskIntegralRecordEntity::getAccountId, riskIntegralRecord.getAccountId());

        wrapper.ge(ObjectUtil.isNotNull(riskIntegralRecord.getBeginTime()), RiskIntegralRecordEntity::getStatisticalDate, riskIntegralRecord.getBeginTime())
                .le(ObjectUtil.isNotNull(riskIntegralRecord.getEndTime()), RiskIntegralRecordEntity::getStatisticalDate, riskIntegralRecord.getEndTime());

        Page pageInfo = riskIntegralRecordService.page(page, wrapper);
        List<RiskIntegralRecordVO> voList = new ArrayList<>();
        for (Object record : pageInfo.getRecords()) {
            RiskIntegralRecordVO vo = new RiskIntegralRecordVO();
            BeanUtils.copyProperties(record, vo);
            voList.add(vo);
        }
        pageInfo.setRecords(voList);
        return R.ok(pageInfo);
    }

    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping(value = "listMyRecord")
    @PreAuthorize("@pms.hasPermission('basic_integral_record_view')")
    public R listMyRecordRecord(Page page, RiskIntegralRecordDTO riskIntegralRecord) {
        LambdaQueryWrapper<RiskIntegralRecordEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(RiskIntegralRecordEntity::getStatisticalDate);
        wrapper.eq(RiskIntegralRecordEntity::getAccountId, SecurityUtils.getUser().getId());
        wrapper.eq(ObjectUtil.isNotNull(riskIntegralRecord.getEventName()), RiskIntegralRecordEntity::getEventName, riskIntegralRecord.getEventName());

        wrapper.ge(ObjectUtil.isNotNull(riskIntegralRecord.getBeginTime()), RiskIntegralRecordEntity::getStatisticalDate, riskIntegralRecord.getBeginTime())
                .le(ObjectUtil.isNotNull(riskIntegralRecord.getEndTime()), RiskIntegralRecordEntity::getStatisticalDate, riskIntegralRecord.getEndTime());

        Page pageInfo = riskIntegralRecordService.page(page, wrapper);
        List<RiskIntegralRecordVO> voList = new ArrayList<>();
        for (Object record : pageInfo.getRecords()) {
            RiskIntegralRecordVO vo = new RiskIntegralRecordVO();
            BeanUtils.copyProperties(record, vo);
            voList.add(vo);
        }
        pageInfo.setRecords(voList);
        return R.ok(pageInfo);
    }


    /**
     * 通过id查询个人积分记录
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('basic_integral_record_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(riskIntegralRecordService.getById(id));
    }

    /**
     * 新增个人积分记录
     *
     * @param riskIntegralRecord 个人积分记录
     * @return R
     */
    @Operation(summary = "新增个人积分记录", description = "新增个人积分记录")
    @SysLog("新增个人积分记录")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('basic_integral_record_add')")
    public R save(@RequestBody RiskIntegralRecordEntity riskIntegralRecord) {
        return R.ok(riskIntegralRecordService.save(riskIntegralRecord));
    }

    /**
     * 修改个人积分记录
     *
     * @param riskIntegralRecord 个人积分记录
     * @return R
     */
    @Operation(summary = "修改个人积分记录", description = "修改个人积分记录")
    @SysLog("修改个人积分记录")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('basic_integral_record_edit')")
    public R updateById(@RequestBody RiskIntegralRecordEntity riskIntegralRecord) {
        return R.ok(riskIntegralRecordService.updateById(riskIntegralRecord));
    }

    /**
     * 通过id删除个人积分记录
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除个人积分记录", description = "通过id删除个人积分记录")
    @SysLog("通过id删除个人积分记录")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('basic_integral_record_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskIntegralRecordService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param riskIntegralRecord 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('basic_integral_record_export')")
    public List<RiskIntegralRecordEntity> export(RiskIntegralRecordEntity riskIntegralRecord) {
        return riskIntegralRecordService.list(Wrappers.query(riskIntegralRecord));
    }
}
